package com.itheima.health.service.impl;

import com.itheima.health.dao.OrdersettingDao;
import com.itheima.health.dto.OrderSettingDTO;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 预约设置业务层实现类
 *
 * @author liuqi
 * @Date 2023/11/20
 */
@Service
public class OrdersettingServiceImpl implements OrdersettingService {
    /**
     * 注入预约设置持久层对象
     */
    @Autowired
    private OrdersettingDao ordersettingDao;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * ⽂件上传批量进⾏预约设置
     */
    @Override
    public void upload(List<OrderSetting> orderSettings) {
        // 遍历插入
        for (OrderSetting orderSetting : orderSettings) {
            // 1.根据日期查询数据
             OrderSetting os  = ordersettingDao.seleteByOrderDate(simpleDateFormat.format(orderSetting.getOrderDate()));

            // 2.没有查询到数据，执行插入操作
            if (os == null) {
                ordersettingDao.insert(orderSetting);
             } else {
                // 3.查询到数据，执行修改操作
                ordersettingDao.update(orderSetting.getNumber(), simpleDateFormat.format(orderSetting.getOrderDate()));
            }
        }
    }

    /**
     * 体检预约-基于⽇历实现预约设置
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        // 根据时间获取数据
        OrderSetting os = ordersettingDao.seleteByOrderDate(simpleDateFormat.format(orderSetting.getOrderDate()));
        // 2.没有查询到数据，执行插入操作
        if (os == null) {
            ordersettingDao.insert(orderSetting);
        } else {
            // 3.查询到数据，执行修改操作
            ordersettingDao.update(orderSetting.getNumber(), simpleDateFormat.format(orderSetting.getOrderDate()));
        }



        // 获取可预约人数
        int number = orderSetting.getNumber();
        // 获取日期字符串
        Date orderDate = orderSetting.getOrderDate();
        String orderDateStr = simpleDateFormat.format(orderDate);
        ordersettingDao.update(number, orderDateStr);
    }

    /**
     * 日历展示预约设置信息
     *
     * @param year  年份
     * @param month 月份
     * @return 预约设置信息
     */
    @Override
    public List<OrderSettingDTO> getOrderSettingByMonth(String year, String month) {
        // 2. 获取每个月的结束日期
        // 获取一个LocalDate对象
        LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), 1);
        // 获取本月的最大天数
        int maxDay = localDate.lengthOfMonth();
        // 结果集合
        List<OrderSettingDTO> orderSettingDTOList = new ArrayList<>();
        for (int i = 0; i < maxDay; i++) {
            String date = year + "-" + month + "-";
            if (i < 9) {
                date += "0" + (i + 1);
            } else {
                date += (i + 1);
            }
            // 调用Dao层获取每天预约设置信息
            OrderSetting orderSetting = this.ordersettingDao.seleteByOrderDate(date);
            if (orderSetting != null) {
                // 返回给前端的预约设置信息
                OrderSettingDTO orderSettingDTO = new OrderSettingDTO();
                // 天
                orderSettingDTO.setDate(i + 1);
                // 可预约人数
                orderSettingDTO.setNumber(orderSetting.getNumber());
                // 已预约人数
                orderSettingDTO.setReservations(orderSetting.getReservations());
                // 将封装后的数据添加到集合中
                orderSettingDTOList.add(orderSettingDTO);
            }
        }
        // 返回数据
        return orderSettingDTOList;
    }
}
